package web.exercise.Dao.impl;

import java.util.ArrayList;
import java.util.List;

import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.Node;

import web.exercise.Dao.ContactsDao;
import web.exercise.entity.Contacts;
import web.exercise.util.XmlOperationUtil;

public class ContactsDaoImpl implements ContactsDao {
	@Override
	public boolean addContacts(Contacts contacts) {
		Document doc=XmlOperationUtil.getDocument();
		String xpath="//contact[@id='"+contacts.getId()+"']";
		Node node=doc.selectSingleNode(xpath);
		if(node!=null){
			return false;
		}else{
			Element root=doc.getRootElement();
			Element elem=root.addElement("contact");
			elem.addAttribute("id", contacts.getId());
			elem.addElement("name").addText(contacts.getName());
			elem.addElement("gender").addText(contacts.getGender());
			elem.addElement("age").addText(contacts.getAge());
			elem.addElement("telephone").addText(contacts.getTelephone());
			elem.addElement("QQ").addText(contacts.getQQ());
			elem.addElement("email").addText(contacts.getEmail());
			//写入
			XmlOperationUtil.write(doc);
			return true;
		}
	}
	//获取所有对象
	@Override
	public ArrayList<Contacts> getContactsList() {
		//读取xml
		Document doc=XmlOperationUtil.getDocument();
		Element root=doc.getRootElement();
		List<Element> elems=root.elements();
		Contacts contacts=null;
		ArrayList<Contacts> contactsList=new ArrayList<Contacts>();
		if(elems==null){
			return null;
		}else{
			for(Element elem:elems){
				contacts=new Contacts();
				contacts.setId(elem.attributeValue("id"));
				contacts.setName(elem.elementText("name"));
				contacts.setAge(elem.elementText("age"));
				contacts.setGender(elem.elementText("gender"));
				contacts.setTelephone(elem.elementText("telephone"));
				contacts.setQQ(elem.elementText("QQ"));
				contacts.setEmail(elem.elementText("email"));
				contactsList.add(contacts);
			}
			return contactsList;
		}
	}

	@Override
	public Contacts getContactsById(String id) {
		ArrayList<Contacts> list=this.getContactsList();
		for(Contacts c:list){
			if(c.getId().equals(id)){
				return c;
			}
		}
		return null;
	}

	@Override
	public void modify(Contacts con) {
		Document doc=XmlOperationUtil.getDocument();
		Element root=doc.getRootElement();
		String xpath="//contact[@id='"+con.getId()+"']";
		Element contact=(Element)root.selectSingleNode(xpath);
		//封装
		contact.element("name").setText(con.getName());
		contact.element("gender").setText(con.getGender());
		contact.element("age").setText(con.getAge());
		contact.element("telephone").setText(con.getTelephone());
		contact.element("QQ").setText(con.getQQ());
		contact.element("email").setText(con.getEmail());
		//写入xml
		XmlOperationUtil.write(doc);
	}

	@Override
	public void delete(String id) {
		Document doc=XmlOperationUtil.getDocument();
		String xpath="//contact[@id='"+id+"']";
		Element contact=(Element)doc.selectSingleNode(xpath);
		if(contact!=null){
			contact.detach();
			XmlOperationUtil.write(doc);
		}
	}
	@Override
	public boolean judge(String id, String name) {
		//读取xml文件
		Contacts contacts=this.getContactsById(id);
		if(contacts!=null||name==""){
			return true;
		}
		List<Contacts> list=this.getContactsList();
		for(Contacts c:list){
			if(c.getName().equals(name)){
				//姓名重复
				return true;
			}
		}
		return false;
	}
}
